package com.jiai.cloud.api;

import com.jiai.cloud.entity.TApplication;
import com.jiai.cloud.entity.TOrder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jiai.cloud.base.exception.ServiceException;
import com.jiai.cloud.base.page.CustomPage;
import com.jiai.cloud.base.tools.RestResultGenerator;


import com.jiai.cloud.feign.OrderAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with j360-cloud-all -> me.j360.cloud.eurekaclient.api.
 * User: min_xu
 * Date: 2015/10/9
 * Time: 10:53
 * 说明：
 */

@RestController
@RequestMapping("/api/v1/order")
public class APIOrderController {
   @Autowired
   private OrderAPI orderAPI;


    /**
     * 直接调用feign，feign会去调用eurekaService
     * */

    //增
    @RequestMapping(value="/addOrder", method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object saveOrder(@RequestBody TOrder tOrder){
        orderAPI.addOrder(tOrder);
        return RestResultGenerator.genSuccessResult("");
    }

    //删
    @RequestMapping(value = "/del/{orderNo}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object deleteNo(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try{
            orderAPI.delete(orderNo);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e){
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }

    //根据订单查找
    @RequestMapping(value = "/findByid/{orderNo}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object findByid(@PathVariable("orderNo") String orderNo) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(orderAPI.selectById(orderNo));
        } catch (Exception e) {
            throw new ServiceException("chaxun", e.getMessage());
        }
    }

    //查找所有的
    @RequestMapping(value = "/findByAll/{page}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"})
    public Object findOrder(@PathVariable("page") Integer page1) throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(orderAPI.selectPage(page1));
        } catch (Exception e) {
            throw new ServiceException("chaxun", e.getMessage());
        }
    }

    //   保存和 更新
    @RequestMapping(value="/edit",method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object edit(@RequestBody TOrder tOrder)throws ServiceException{
        try {
            orderAPI.edit(tOrder);
            return RestResultGenerator.genSuccessResult() ;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }



}
